package com.zrrd.yunchmall.user.controller;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.user.entity.Menu;
import com.zrrd.yunchmall.user.entity.ResourceCategory;
import com.zrrd.yunchmall.user.service.IResourceCategoryService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 资源分类表 前端控制器
 * </p>
 *
 * @author YeKong
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/resourceCategory")
@ApiOperation("权限服务-查询全部资源")
public class ResourceCategoryController {


    @Autowired
    private IResourceCategoryService resourceCategoryService;

    @ApiOperation("展示全部资源")
    @GetMapping("/listAll")
    public ResponseResult list() {
        List<ResourceCategory> categories = resourceCategoryService.list();
        return new ResponseResult(200,"查询成功",
                categories);
    }

    @ApiOperation("删除资源模块")
    @PostMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") long id) {
        resourceCategoryService.removeById(id);
        return new ResponseResult(200,"删除成功");
    }

    @ApiOperation("添加一个资源模块")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody ResourceCategory resourceCategory){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", resourceCategory.getName());
        if (resourceCategoryService.count(queryWrapper) > 0) {
            return new ResponseResult(205,"注册失败，用户名已存在");
        }

        queryWrapper.eq("sort",resourceCategory.getSort());
        //设置创建的时间
        resourceCategory.setCreateTime(LocalDateTime.now());
        //在数据库中保存管理员对象

        resourceCategoryService.save(resourceCategory);
        return new ResponseResult(200,"注册成功");
    }

    @ApiOperation("修改资源模块信息")
    @PostMapping("/update/{id}")
    public ResponseResult update(@RequestBody ResourceCategory resourceCategory, @PathVariable("id") long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("name",resourceCategory.getName());
        updateWrapper.eq("sort",resourceCategory.getSort());
        resourceCategoryService.update(resourceCategory,updateWrapper);
        return new ResponseResult(200,"修改成功");
    }


}
